package com.coolgor.coldot.web.auth;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.coolgor.coldot.enums.auth.LocalUserOperationStateEnum;
import com.coolgor.coldot.dto.auth.LocalUserExecution;
import com.coolgor.coldot.entity.auth.LocalUser;
import com.coolgor.coldot.enums.auth.LocalUserStateEnum;
import com.coolgor.coldot.service.auth.LocalUserService;
import com.coolgor.coldot.common.HttpServletRequestUtil;


@Controller
@RequestMapping(value="auth", method={RequestMethod.GET,RequestMethod.POST})
public class LocalUserController {
	@Autowired
	private LocalUserService localUserService;

	/**
	 * 获取用户列表
	 */
	@RequestMapping(value = "/getLocalUserList", method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	private Map<String, Object> getLocalUserList(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		int page = 0 ;
		int pageSize = 0;
		try {
			page = Integer.parseInt(request.getParameter("page"));
			pageSize = Integer.parseInt(request.getParameter("rows"));
		} catch (Exception e) {
			modelMap.put("success", false);
			modelMap.put("errMsg", e.toString());
		}
		LocalUser localUserCondition = new LocalUser();
		localUserCondition.setUsername(HttpServletRequestUtil.getString(request,"username"));
		localUserCondition.setNameCN(HttpServletRequestUtil.getString(request,"name_cn"));
		localUserCondition.setStatus(HttpServletRequestUtil.getInteger(request,"status"));
		LocalUserExecution exe = localUserService.getLocalUserList(localUserCondition, page, pageSize);
		if (null != exe | exe.getState()== LocalUserOperationStateEnum.SUCCESS.getState()) {
			try {
				modelMap.put("total",exe.getCount());
				modelMap.put("rows",exe.getLocalUserList());
				modelMap.put("success", true);
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
			}
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "Empty List");
		}
		return modelMap;
	}

	/**
	 * 新增用户
	 */
	@RequestMapping(value = "/addLocalUser", method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	private Map<String, Object> addLocalUser(@RequestBody LocalUser localUser, HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		if (null!=localUser.getUserID()) localUser.setUserId(null);
		LocalUserExecution exe = localUserService.addLocalUser(localUser);
		if (null != exe && exe.getState()== LocalUserOperationStateEnum.SUCCESS.getState()) {
			try {
				modelMap.put("success", true);
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", "操作失败："+e.toString());
			}
		} else if (null != exe && exe.getState() == LocalUserOperationStateEnum.EXISTUSERNAME.getState()) {
			modelMap.put("success", false);
			modelMap.put("errMsg", "数据库中已存在该用户名，操作失败！");
		} else if (null != exe && exe.getState() == LocalUserOperationStateEnum.NULL_USER.getState()) {
			modelMap.put("success", false);
			modelMap.put("errMsg", "用户信息为空，无法更新！");
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "未知错误，操作失败！");
		}
		return modelMap;
	}

	/**
	 * 修改用户信息
	 */
	@RequestMapping(value = "/modifyLocalUser", method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	private Map<String, Object> modifyLocalUser(@RequestBody LocalUser localUser, HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		if (null==localUser.getUserID() || localUser.getUserID()<=0L) {
			modelMap.put("success", false);
			modelMap.put("errMsg", "UserID未能传递到后台！");
			return modelMap;
		}
		LocalUserExecution exe = localUserService.modifyLocalUser(localUser);
		if (null != exe && exe.getState()== LocalUserOperationStateEnum.SUCCESS.getState()) {
			try {
				modelMap.put("success", true);
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", "操作失败："+e.toString());
			}
		} else if (null != exe && exe.getState()== LocalUserOperationStateEnum.EXISTUSERNAME.getState()) {
			modelMap.put("success", false);
			modelMap.put("errMsg", "数据库中已存在该用户名，操作失败！");
		} else if (null != exe && exe.getState()== LocalUserOperationStateEnum.NULL_USER.getState()) {
			modelMap.put("success", false);
			modelMap.put("errMsg", "用户信息为空，无法更新！");
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "未知错误，操作失败！");
		}
		return modelMap;
	}

	/**
	 * 修改用户密码
	 */
	@RequestMapping(value = "/modifyPassword", method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	private Map<String, Object> modifyPassword(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		LocalUser localUser = new LocalUser();
		localUser.setUserId(HttpServletRequestUtil.getLong(request,"user_id"));
		if (localUser.getUserID()==-1L) {
			modelMap.put("success", false);
			modelMap.put("errMsg", "UserID未能传递到后台！");
			return modelMap;
		}
		localUser.setPassword(HttpServletRequestUtil.getString(request,"password"));
		if (null == localUser.getPassword()) localUser.setPassword("");
		LocalUserExecution exe = localUserService.modifyLocalUser(localUser);
		if (null != exe && exe.getState()== LocalUserOperationStateEnum.SUCCESS.getState()) {
			try {
				modelMap.put("success", true);
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", "操作失败："+e.toString());
			}
		} else if (null != exe && exe.getState()== LocalUserOperationStateEnum.NULL_USER.getState()) {
			modelMap.put("success", false);
			modelMap.put("errMsg", "用户信息为空，无法更新！");
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "未知错误，操作失败！");
		}
		return modelMap;
	}

	/**
	 * 修改用户状态
	 */
	@RequestMapping(value = "/changeLocalUserStatus", method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	private Map<String, Object> changeLocalUserStatus(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		LocalUser localUser = new LocalUser();
		localUser.setUserId(HttpServletRequestUtil.getLong(request,"user_id"));
		if (localUser.getUserID()==-1L) {
			modelMap.put("success", false);
			modelMap.put("errMsg", "UserID未能传递到后台！");
			return modelMap;
		}
		localUser.setStatus(HttpServletRequestUtil.getInteger(request,"status"));
		LocalUserExecution exe = localUserService.modifyLocalUser(localUser);
		if (null != exe && exe.getState()== LocalUserOperationStateEnum.SUCCESS.getState()) {
			try {
				modelMap.put("success", true);
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", "操作失败："+e.toString());
			}
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "未知错误，操作失败！");
		}
		return modelMap;
	}

	/**
	 * 删除用户信息
	 */
	@RequestMapping(value = "/removeLocalUser", method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	private Map<String, Object> removeLocalUser(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		LocalUser localUser = new LocalUser();
		localUser.setUserId(HttpServletRequestUtil.getLong(request,"user_id"));
		if (localUser.getUserID()<=0L) {
			modelMap.put("success", false);
			modelMap.put("errMsg", "UserID未能传递到后台！");
			return modelMap;
		}
		LocalUserExecution exe = localUserService.removeLocalUser(localUser);
		if (null != exe && exe.getState()== LocalUserOperationStateEnum.SUCCESS.getState()) {
			try {
				modelMap.put("success", true);
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", "操作失败："+e.toString());
			}
		} else if (null != exe && exe.getState()== LocalUserOperationStateEnum.NULL_USER.getState()) {
			modelMap.put("success", false);
			modelMap.put("errMsg", "用户信息为空，无法删除！");
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "未知错误，操作失败！");
		}
		return modelMap;
	}

	/**
	 * 将用户状态enum转换成json送至前端
	 */
	@RequestMapping(value = "/getLocalUserStateEnum", method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	private Map<String, Object> getLocalUserStateEnum(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("success", true);
		modelMap.put("data", LocalUserStateEnum.toJsonString());
		return modelMap;
	}

}
